<template>
  <el-form ref="loginForm" :model="loginForm" :rules="loginRules">
    <el-form-item prop="username">
      <el-input
        v-model="loginForm.username"
        type="text"
        size="large"
        auto-complete="off"
        placeholder="请输入账号"
      />
    </el-form-item>
    <el-form-item prop="password">
      <el-input
        v-model="loginForm.password"
        type="password"
        auto-complete="off"
        size="large"
        placeholder="请输入密码"
        @keyup.enter.native="handleLogin"
      ></el-input>
    </el-form-item>
    <el-form-item prop="code" v-if="captchaEnabled">
      <el-input
        v-model="loginForm.code"
        auto-complete="off"
        placeholder="请输入验证码"
        style="width: 57%"
        size="large"
        @keyup.enter.native="handleLogin"
      ></el-input>
      <div class="login-code">
        <img :src="codeUrl" @click="getCode" class="login-code-img" />
      </div>
    </el-form-item>
    <el-form-item style="width: 100%">
      <el-button
        :loading="loading"
        type="primary"
        style="width: 100%; height: 40px; font-size: 18px"
        @click.native.prevent="handleLogin"
      >
        <span v-if="!loading">立即登录</span>
        <span v-else>登 录 中...</span>
      </el-button>
      <!-- <div style="float: right;" v-if="!register">
                <router-link class="link-type" :to="'/register'">立即注册</router-link>
            </div> -->
      <div style="display: flex; align-items: center; justify-content: space-between">
        <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
        <div>
          <router-link class="link-type" to="/reset-password">忘记密码?</router-link>
        </div>
      </div>
    </el-form-item>
  </el-form>
</template>

<script>
import { getCodeImg } from '@/api/login';
import Cookies from 'js-cookie';
import { encrypt, decrypt } from '@/utils/jsencrypt';

export default {
  name: 'PasswordForm',
  data() {
    return {
      codeUrl: '',
      loginForm: {
        username: '',
        password: '',
        rememberMe: false,
        code: '',
        uuid: '',
        loginType: '0',
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '请输入您的账号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入您的密码' }],
        code: [{ required: true, trigger: 'change', message: '请输入验证码' }],
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined,
    };
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      let _this = this;
      getCodeImg().then((res) => {
        _this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (_this.captchaEnabled) {
          _this.codeUrl = 'data:image/gif;base64,' + res.img;
          _this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get('username');
      const password = Cookies.get('password');
      const rememberMe = Cookies.get('rememberMe');
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
        loginType: '0',
      };
    },
    handleLogin() {
      let _this = this;
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          _this.loading = true;
          if (_this.loginForm.rememberMe) {
            Cookies.set('username', _this.loginForm.username, { expires: 30 });
            Cookies.set('password', encrypt(_this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', _this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove('username');
            Cookies.remove('password');
            Cookies.remove('rememberMe');
          }
          _this.$store
            .dispatch('Login', _this.loginForm)
            .then(() => {
              _this.$router.push({ path: '/' }).catch(() => {});
            })
            .catch(() => {
              _this.loading = false;
              if (_this.captchaEnabled) {
                _this.getCode();
              }
            });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
:deep(.el-input) {
  height: 38px;
}

:deep(.el-input .input) {
  height: 38px;
}

:deep(.input-icon) {
  height: 39px;
  width: 14px;
  margin-left: 2px;
}
:deep(.login-tip) {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;
}

img {
  cursor: pointer;
  vertical-align: middle;
}
.login-code-img {
  height: 38px;
}
</style>
